package com.swz.dcrm.ui.viewmodel;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MediatorLiveData;

import com.swz.dcrm.api.DcrmApi;
import com.swz.dcrm.db.AppDatabase;
import com.swz.dcrm.db.TbCarModel;
import com.swz.dcrm.model.BaseResponse;
import com.swz.dcrm.model.CarBrand;
import com.swz.dcrm.model.CarModel;
import com.swz.dcrm.model.CarSeries;
import com.swz.dcrm.model.Page;
import com.swz.dcrm.model.PageResponse;
import com.xh.baselibrary.callback.BaseViewModel;
import com.xh.baselibrary.callback.CallBack;

import java.util.List;

import retrofit2.Retrofit;

public class CarBrandViewModel extends BaseViewModel {
    private Retrofit mRetrofit;
    private DcrmApi dcrmApi;

    private MediatorLiveData<BaseResponse<List<CarBrand>>>carBrands;
    private MediatorLiveData<BaseResponse<List<CarSeries>>>carSeries;
    private MediatorLiveData<PageResponse<CarModel>>carModels;


    private MediatorLiveData<CarBrand> selectedCarBrand;
    private MediatorLiveData<CarSeries> selectedCarSeries;
    private MediatorLiveData<CarModel> selectedCarModel;
    private AppDatabase appDatabase;
    public CarBrandViewModel(Retrofit retrofit, AppDatabase appDatabase){
        this.mRetrofit=retrofit;
        this.appDatabase=appDatabase;
        dcrmApi=mRetrofit.create(DcrmApi.class);
    }

    public MediatorLiveData<BaseResponse<List<CarBrand>>> getCarBrands(){
        carBrands=creatLiveData(carBrands);
        dcrmApi.getCarBrand()//"initials_asc"
                .enqueue(new CallBack<>(this,carBrands));
        return carBrands;
    }

    public MediatorLiveData<BaseResponse<List<CarSeries>>> getCarSeries(long brandId) {
        carSeries=creatLiveData(carSeries);
        dcrmApi.getCarSeries(brandId).enqueue(new CallBack<>(this,carSeries));
        return carSeries;
    }

    public MediatorLiveData<PageResponse<CarModel>> getCarModels(int page, int size, long seriesId) {
        carModels=creatLiveData(carModels);
        dcrmApi.getCarModel(page,size,seriesId)
                .enqueue(new CallBack<>(this,carModels));
        return carModels;
    }
    public void insertSearch(TbCarModel tbCarModel){
        try {
            appDatabase.carModelDao().insertSingle(tbCarModel);
        }catch (Exception e){

        }

    }

    public LiveData<List<TbCarModel>>getLatelySearch(){
        return appDatabase.carModelDao().getTbCarModels();
    }

    public MediatorLiveData<CarBrand> getSelectedCarBrand() {
        selectedCarBrand=creatLiveData(selectedCarBrand);
        return selectedCarBrand;
    }

    public void delLatelySearchById(TbCarModel tbCarModel){
        appDatabase.carModelDao().delete(tbCarModel);
    }
    public void delAllLatelySearch(List<TbCarModel>list){
        appDatabase.carModelDao().deleteAll(list);
    }

    public MediatorLiveData<CarSeries> getSelectedCarSeries() {
        selectedCarSeries=creatLiveData(selectedCarSeries);
        return selectedCarSeries;
    }

    public MediatorLiveData<CarModel> getSelectedCarModel() {
        selectedCarModel=creatLiveData(selectedCarModel);
        return selectedCarModel;
    }
}
